package com.zoespeer.food_delivery_comment_api.controller;

import com.zoespeer.food_delivery_comment_api.common.ApiResponse;
import com.zoespeer.food_delivery_comment_api.service.ReviewService;
import com.zoespeer.food_delivery_comment_api.util.TokenParseUtil;
import com.zoespeer.food_delivery_comment_api.vo.MyReviewVO;
import com.zoespeer.food_delivery_comment_api.vo.PublishCommandRequestVO;
import com.zoespeer.food_delivery_comment_api.vo.UserIdResponseVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论相关接口控制器：根据场景调用不同类型的Token解析方法
 */
@RestController
@RequestMapping("/api/review")
public class ReviewController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 发布评论接口（需Integer类型用户ID）
     */
    @PostMapping("/publishCommand")
    public ApiResponse<Object> publishCommand(
            @RequestHeader("Authorization") String token,
            @Valid @RequestBody PublishCommandRequestVO request
    ) {
        ApiResponse<Integer> userIdResp = TokenParseUtil.parseUserIdAsInt(token);
        if (userIdResp.getCode() != 200) {
            return ApiResponse.error(userIdResp.getMessage());
        }
        Integer userId = userIdResp.getData();

        boolean result = reviewService.publishCommand(request, userId);
        return result ? ApiResponse.success(null, "发布评论成功") : ApiResponse.error("发布评论失败");
    }

    /**
     * 获取当前用户的所有评论接口（通过请求参数传入userId）
     */
    @GetMapping("/getMyReview")
    public ApiResponse<List<MyReviewVO>> getMyReview(
            @RequestParam("userId") String userId // 从请求参数接收userId
    ) {
        // 校验userId合法性（非空、格式等）
        if (userId == null || userId.trim().isEmpty()) {
            return ApiResponse.error("用户ID不能为空");
        }
        try {
            List<MyReviewVO> myReviews = reviewService.getMyReviews(userId);
            return ApiResponse.success(myReviews, "查询我的评论成功");
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    // 原删除评论接口方法
    @DeleteMapping("/deleteReview")
    public ApiResponse<Map<String, Object>> deleteReview(
            @RequestHeader("Authorization") String token,
            @RequestParam("reviewId") Integer reviewId
    ) {
        ApiResponse<Integer> userIdResp = TokenParseUtil.parseUserIdAsInt(token);
        if (userIdResp.getCode() != 200) {
            return ApiResponse.error(userIdResp.getMessage());
        }
        Integer userId = userIdResp.getData();

        if (reviewId == null || reviewId <= 0) {
            return ApiResponse.error("评论ID无效，请传入正整数");
        }

        try {
            reviewService.deleteReview(reviewId, userId);
            return ApiResponse.success(new HashMap<>(), "评论删除成功"); // 返回空Map
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
    /**
     * 用评论ID获取对应用户ID（返回封装VO）
     */
    @GetMapping("/getuserId")
    public ApiResponse<UserIdResponseVO> getUserIdByReviewId(
            @RequestParam("reviewId") Integer reviewId // 参数改为Integer，直接接收数字
    ) {
        try {
            UserIdResponseVO responseVO = reviewService.getUserIdByReviewId(reviewId);
            return ApiResponse.success(responseVO, "获取用户ID成功");
        } catch (RuntimeException e) {
            return ApiResponse.error(e.getMessage());
        }
    }
}